<?php
// +----------------------------------------------------------------------
// | ebSIG
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.ebsig.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: liudaojian <liudaojian@ebsig.com>
// +----------------------------------------------------------------------

/**
 * ERP接口类
 * @author   liudaojian <liudaojian@ebsig.com>
 * @version 1.0
 */

namespace App\Service\Rpc;

class Erp
{

    private $instance;

    public function __construct()
    {

        //项目标识
        $project_index = config('system.project_index');

        if ($project_index) {

            $project_index = ucfirst($project_index);

            //根据项目标识，实例相应类
            $class_name = __NAMESPACE__  . '\Erp\\' .$project_index . 'Erp';
            $this->instance = new $class_name;

        }

    }

    /**
     * 推送订单信息到erp
     * @return mixed
     */
    public function pushToErp()
    {

        if (!is_object($this->instance)) {
            return ['code' => 404, 'message' => '实例对象没有找到'];
        }

        return $this->instance->pushToErp();

    }

    /**
     * 生成订单传送给线下
     * @param $args_array
     * @return array
     */
    public function createBillPushData($args_array)
    {

        if (!is_object($this->instance)) {
            return ['code' => 404, 'message' => '实例对象没有找到'];
        }

        return $this->instance->createBillPushData($args_array);

    }

    /**
     * 推送订单信息到erp系统
     * @param $args_array
     * @return mixed
     */
    public function pushBillToErp($args_array)
    {

        if (!is_object($this->instance)) {
            return ['code' => 404, 'message' => '实例对象没有找到'];
        }

        return $this->instance->pushBillToErp($args_array);

    }

    /**
     * 订单退款退货
     * @param $args_array
     * @return mixed
     */
    public function refundrtnreq($args_array)
    {

        if (!is_object($this->instance)) {
            return ['code' => 404, 'message' => '实例对象没有找到'];
        }

        return $this->instance->refundrtnreq($args_array);

    }

    /**
     * 取消订单
     * @param $args_array
     * @return mixed
     */
    public function cancelBill($args_array)
    {

        if (!is_object($this->instance)) {
            return ['code' => 404, 'message' => '实例对象没有找到'];
        }

        return $this->instance->cancelBill($args_array);

    }

    /**
     * 重载方法
     * @param $name
     * @param $args_array
     * @return array
     */
    public function __call( $name, $args_array ) {

        if (!is_object($this->instance)) {
            return ['code' => 404, 'message' => '实例对象没有找到'];
        }

        if (!method_exists( $this->instance, $name)) {
            return ['code' => 404, 'message' => '未定义' . $name  . '方法'];
        }

        return $this->instance->$name( $args_array[0] );

    }

}